﻿
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoStoreOrder
    {
        public daoStoreOrder()
        {

        }
        private StoreOrder Load(IDataReader de)
        {
            StoreOrder item = new StoreOrder();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.OrderId = clsConvert.DbToInt("OrderId", de);
            item.TrackingOrder = clsConvert.DbToString("TrackingOrder", de);
            item.PurchaseOrder = clsConvert.DbToString("PurchaseOrder", de);
            item.CustomerId = clsConvert.DbToInt("CustomerId", de);
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.FinishDate = clsConvert.DbToDateTime("FinishDate", de);
            item.ModifiedBy = clsConvert.DbToString("ModifiedBy", de);
            item.ModifiedDate = clsConvert.DbToDateTime("ModifiedDate", de);
           
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }
        public StoreOrder GetByID(int orderId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            StoreOrder item = new StoreOrder();

            try
            {
                para[0] = new SqlParameter(busStoreOrder.OrderId, SqlDbType.Int);
                para[0].Value = orderId;
                using (de = clsSQL.SPDataReader("StoreOrder_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoStoreOrder", "GetByID(int orderId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }


        public StoreOrderCollection List(StoreOrder objOrder)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            StoreOrderCollection items = new StoreOrderCollection();

            try
            {
                para[0] = new SqlParameter(busStoreOrder.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objOrder.OrderBy;
                para[1] = new SqlParameter(busStoreOrder.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objOrder.OrderDirection;
                para[2] = new SqlParameter(busStoreOrder.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objOrder.CurrentPage;
                para[3] = new SqlParameter(busStoreOrder.PAGESIZE, SqlDbType.Int);
                para[3].Value = objOrder.PageSize;
                para[4] = new SqlParameter(busStoreOrder.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objOrder.Condition;
                para[5] = new SqlParameter(busStoreOrder.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("StoreOrder_List", para);
                while (de.Read())
                {
                    StoreOrder item = Load(de);
                    items.Add(item);
                }

                de.Close();
                objOrder.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoStoreOrder", "List(StoreOrder objOrder)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        public bool Delete(int orderId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busStoreOrder.OrderId, SqlDbType.Int);
                para[0].Value = orderId;
                result = clsSQL.SPExecute("StoreOrder_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoStoreOrder", "Delete(int orderId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }

        public int Insert(StoreOrder objOrder)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            try
            {
                para[0] = new SqlParameter(busStoreOrder.TrackingOrder, SqlDbType.NVarChar);
                para[0].Value = objOrder.TrackingOrder;

                para[1] = new SqlParameter(busStoreOrder.PurchaseOrder, SqlDbType.NVarChar);
                para[1].Value = objOrder.PurchaseOrder;

                para[2] = new SqlParameter(busStoreOrder.CustomerId, SqlDbType.Int);
                para[2].Value = clsConvert.IntToDB(objOrder.CustomerId);

                para[3] = new SqlParameter(busStoreOrder.CreatedDate, SqlDbType.DateTime);
                para[3].Value = objOrder.CreatedDate;

                para[4] = new SqlParameter(busStoreOrder.CreatedBy, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objOrder.CreatedBy);

                para[5] = new SqlParameter(busStoreOrder.FinishDate, SqlDbType.DateTime);
                if (objOrder.FinishDate == DateTime.MinValue)
                    para[5].Value = DBNull.Value;
                else
                    para[5].Value = clsConvert.DateToDB(objOrder.FinishDate);

                result = clsSQL.SPExecute("StoreOrder_Insert", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoStoreOrder", "Insert(StoreOrder objOrder)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public int Update(StoreOrder objOrder)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[7];
            try
            {
                para[0] = new SqlParameter(busStoreOrder.TrackingOrder, SqlDbType.NVarChar);
                para[0].Value = objOrder.TrackingOrder;

                para[1] = new SqlParameter(busStoreOrder.PurchaseOrder, SqlDbType.NVarChar);
                para[1].Value = objOrder.PurchaseOrder;

                para[2] = new SqlParameter(busStoreOrder.CustomerId, SqlDbType.Int);
                para[2].Value = clsConvert.IntToDB(objOrder.CustomerId);

                para[3] = new SqlParameter(busStoreOrder.ModifiedDate, SqlDbType.DateTime);
                para[3].Value = objOrder.ModifiedDate;

                para[4] = new SqlParameter(busStoreOrder.ModifiedBy, SqlDbType.NVarChar);
                para[4].Value = clsConvert.StringToDB(objOrder.ModifiedBy);

                para[5] = new SqlParameter(busStoreOrder.FinishDate, SqlDbType.DateTime);
                if (objOrder.FinishDate == DateTime.MinValue)
                    para[5].Value = DBNull.Value;
                else
                    para[5].Value = clsConvert.DateToDB(objOrder.FinishDate);

                para[6] = new SqlParameter(busStoreOrder.OrderId, SqlDbType.Int);
                para[6].Value = objOrder.OrderId;

                result = clsSQL.SPExecute("StoreOrder_Update", para);
               

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoStoreOrder", "Update(StoreOrder objOrder)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public string GenerateTrackingOrderCode()
        {
            clsSQL clsSQL = new clsSQL();
            SqlDataReader de;
            string result = string.Empty;
            try
            {

                de = clsSQL.SPDataReader("Order_GenerateCode");
                if (de.Read())
                {

                    result = clsConvert.DbToString("Code", de);
                }
                de.Close();
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoStoreOrder", "GenerateTrackingOrderCode", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return result;
        }
    }
}
